package com.ddframework.orm.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.ddframework.tableinfo.Id;
import com.ddframework.tableinfo.ManytoOne;
import com.ddframework.tableinfo.OnetoMany;
import com.ddframework.tableinfo.Property;
import com.ddframework.tableinfo.TableInfo;

/**
 * Read Configuration
 * 
 * @author Wang Ding
 * 
 */
public class XMLFactory {

	public static Map<String, String> getXMLInfo(String filename) {
		Map<String, String> jdbcmap = new HashMap<String, String>();
		File file = new File(filename);
		SAXReader reader = new SAXReader();
		Document doc = null;
		try {
			doc = reader.read(file);
			Element roots = doc.getRootElement();
			Element datasource = roots.element("datasource");
			Element jdbc = datasource.element("jdbc");
			List<Element> properties = jdbc.elements();
			for (Element property : properties) {
				Attribute nameAttr = property.attribute("name");
				Attribute valueAttr = property.attribute("value");
				if (valueAttr != null) {
					//both name and value exist
					jdbcmap.put(nameAttr.getText(), valueAttr.getText());

				} else {
					//name exists,but value doesn't
					jdbcmap.put(nameAttr.getText(), property.getText());

				}
			}
			Constant.DBMAP = jdbcmap;
			readBeanXML(roots);
		} catch (DocumentException e) {
			e.printStackTrace();
		}

		return jdbcmap;
	}
	
	private static void readBeanXML(Element roots){
		Element ormmapping=roots.element("orm-mapping");
		Element listelement=ormmapping.element("list");
		@SuppressWarnings("unchecked")
		List<Element> valuelist=listelement.elements();
		List<String> filelist=new ArrayList<String>();
		for(Element e:valuelist){
			filelist.add(e.getText());
		}
		readTableXMLInfo(filelist);
		
		
	}
	
	private static void readTableXMLInfo(List<String> filelist){
		
		Constant.TABLEMAP = TableInfoXMLUtils.getTableInfoXML(filelist);
	}

	public static void main(String[] args) {
		String file = XMLFactory.class.getResource("/ddorm.datasource.xml").getFile();
	
	System.out.println(file);
	getXMLInfo(file);
	Map<String,TableInfo> map = Constant.TABLEMAP;
	Set<String> set = map.keySet();
	Iterator<String> it = set.iterator();
	while(it.hasNext()){
		String key=it.next();
		TableInfo table=map.get(key);
		System.out.println("Config File's Info");
		System.out.println("classname:"+table.getClassname()+" tablename:"+table.getTablename());
		Id id=table.getId();
		System.out.println("Primary Key : idname:"+id.getIdname()+" idcolumn:"+id.getIdcolumn());
		List<Property> plist=table.getPlist();
		if(plist!=null){
			System.out.println("Property's content:");
			for(Property p:plist){
				System.out.println("pname:"+p.getPropertyname()+" pcolumn:"+p.getPropertycolumn());
			}
		}
		List<OnetoMany> onelist=table.getOnelist();
		if(onelist!=null){
			System.out.println("One to Many:");
			for(OnetoMany one:onelist){
				System.out.println("onename:"+one.getOnename()+" oneclass:"+one.getOneclass()+" onecolumn:"+one.getOnecolumn());
			}
		}
		List<ManytoOne> manylist=table.getManylist();
		if(manylist!=null){
			System.out.println("Many To One:");
			for(ManytoOne many:manylist){
				System.out.println("manyname:"+many.getManyname()+" manyclass:"+many.getManyclass()+" manycolumn:"+many.getManycolumn());
			}
		}
		System.out.println();
	}

	}

}
